//
//  IssueGoodsView.swift
//  IdleFish
//
//  Created by Ben on 2021/10/20.
//

import SwiftUI

struct IssueGoodsView: View {
    
    @Binding var showIssueGoods: Bool
    
    @State var showExecMenu: Bool = false
    
    @State var issueBtnAngle: CGFloat = 0
    
    var body: some View {
        
        
        VStack(spacing: 50) {
            
            Spacer()
                            
            VStack(alignment: .leading, spacing: 35) {
                
                
                IssueExecView(icon: "issue_play_doc", title: "发会玩帖子", subTitle: "分享我的趣味生活")
                IssueExecView(icon: "issue_tao", title: "一键转卖", subTitle: "立即转卖换钱")
                IssueExecView(icon: "issue_scale", title: "省心卖", subTitle: "高价回收寄卖")
                IssueExecView(icon: "issue_send", title: "发闲置", subTitle: "30s发布宝贝")

            }
            .opacity(showIssueGoods ? 1 : 0)
            .offset(y: showExecMenu ? 0 : 50)
            .offset(x: -25)

            Button {
                withAnimation {
                    showIssueGoods.toggle()
                }
            } label: {
                ZStack {
                    Image(systemName: "plus")
                        .font(.system(size: 20, weight: .semibold))
                        .foregroundColor(.black)
                        .rotation3DEffect(.degrees(issueBtnAngle), axis: (x: 0, y: 0, z: 1))
                }
                .frame(width: 70, height: 70)
                .background(showIssueGoods ? Color.white : Color.yellow)
                .clipShape(Circle())
                    
            }
            .padding(.bottom, 10)

        }

        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(
            Color(hex: 0x353336).opacity(showIssueGoods ? 1 : 0)
        )
        .onChange(of: showIssueGoods) { newValue in
            print(newValue)
            
            withAnimation(.spring(response: 1, dampingFraction: 0.5, blendDuration: 100)) {
                showExecMenu = newValue
            }
            
            withAnimation {
                if newValue == true {
                    issueBtnAngle += 45
                } else {
                    issueBtnAngle -= 45
                }
            }
        }
        .onTapGesture {
            withAnimation {
                showIssueGoods.toggle()
            }
        }
        .opacity(showIssueGoods ? 1 : 0)
    }
}

struct IssueExecView: View {
    
    var icon: String
    var title: String
    var subTitle: String
    
    var body: some View {
        
        HStack {
            
            Image(icon)
                .resize(size: CGSize(width: 70, height: 70), contentModel: .fill)
            
            VStack(alignment: .leading, spacing: 6) {
                Text(title)
                    .font(.system(size: 18, weight: .semibold))
                    .foregroundColor(.white)
                
                Text(subTitle)
                    .font(.system(size: 14))
                    .foregroundColor(Color(hex: 0x949295))
            }
        }
        .frame(alignment: .leading)
    }
}
